package com.yishu.servlet;

import com.yishu.Dao.UserDao;
import com.yishu.entity.User;
import com.yishu.util.DBConnection;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;

public class ShowAUserServlet extends HttpServlet{

    UserDao userDao = new UserDao();
    DBConnection dbConnection = new DBConnection();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int userId = Integer.parseInt(req.getParameter("userId"));
        Connection connection = null;

        try {
            connection = dbConnection.getCon();
            User user = userDao.showAUser(userId,connection);
            dbConnection.closeCon(connection);

            req.setAttribute("user",user);
            req.getRequestDispatcher("/user/updateUser.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
